package kocla.com.webviewloadlocalimge;

import android.graphics.Bitmap;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    private WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview = (WebView) findViewById(R.id.webview);
        initWebViewSetting(webview);
        String data = "<HTML>在模拟器 2.1 上测试,这是<IMG src=\"file:///android_asset/images/icon_open_video.png\"/>APK里的图片";
        String str = "file:///android_asset/images/icon_open_video.png";
        webview.loadDataWithBaseURL("file:///android_asset/images/"
                , data
                , "text/html"
                , "utf-8"
                , null);

//        webview.loadUrl(str);

    }

    public void initWebViewSetting(WebView webView) {
        //自适应任意大小的pc网页
        WebSettings settings = webView.getSettings();
        //得到文章字体的大小
        int fontSize = 100;
        settings.setTextZoom(fontSize);


        //
        //-------------------------------------------------
        if (Build.VERSION.SDK_INT >= 8) {
            settings.setPluginState(WebSettings.PluginState.ON);
        } else {
        }
        //
//        webView.setInitialScale(100);
        // 设置可以支持缩放
        settings.setSupportZoom(false);
        // 设置出现缩放工具
        settings.setBuiltInZoomControls(false);
//        webView.hideZoomController();
        //扩大比例的缩放
        settings.setUseWideViewPort(false);
        //自适应屏幕
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);

        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);

        //
        webView.setVerticalScrollbarOverlay(false);
        webView.setHorizontalScrollbarOverlay(false);
        //去掉滚动条
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        //
        //-------------------------------------------------
        //Java和Javascript安全交互
        settings.setJavaScriptEnabled(true);// 支持js
        //webView.addJavascriptInterface(new JavaScriptInterface(), "androidlistner");
        // 如果页面中链接，如果希望点击链接继续在当前browser中响应，而不是新开Android的系统browser中响应该链接，必须覆盖webview的WebViewClient对象
        //webView.setWebViewClient(new MyWebViewClient());
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }
        });
        webView.setWebViewClient(new MyWebViewClient());
        webView.requestFocus(View.FOCUS_DOWN | View.FOCUS_UP);


        settings.setLightTouchEnabled(true);

    }

    class MyWebViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }
    }


}
